package com.ocom.payconfig.controller;


import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityPackageData;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.entity.payconfig.EntityPackageType;
import com.ocom.common.entity.payconfig.EntityVoucherData;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.callpackage.*;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;

@Slf4j
@RestController
public class VideoPhoneController {


    @Autowired
    private XfSubAccountListService xfSubAccountListService;
    @Autowired
    private PackageDataService packageDataService;
    @Autowired
    private VoucherDataService voucherDataService;
    @Autowired
    private PackageTypeService packageTypeService;

    @Autowired
    private XfAccountInfoService xfAccountInfoService;

    @PostMapping(PayConfigUrl.Url_querySubAccount)
    public Result querySubAccount(@RequestBody QuerySubAccountRequest request){
        log.info("查询个人钱包  【{}】",request);
        if(request.getSubAccount() == null || request.getSubAccount() == 0){
            QuerySubAccountResponse querySubAccountResponse =new QuerySubAccountResponse();
            EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.GetXFAccount(request.getComId(), request.getPId().intValue(),0);
            querySubAccountResponse.setSubAccount(0);
            querySubAccountResponse.setSubAccountName("默认钱包");
            querySubAccountResponse.setRechargeBalance(entityXfAccountInfo.getAccountRechargeMoney().intValue());
            querySubAccountResponse.setSubsidyBalance(entityXfAccountInfo.getAccountSubsidyMoney().intValue());
            return ResultUtil.success(querySubAccountResponse);
        }

        return ResultUtil.success(xfSubAccountListService.querySubAccount(request.getComId(), request.getPId(), request.getSubAccount()));
    }

    @PostMapping(PayConfigUrl.Url_getPackagePay)
    public Result getPackagePay(@RequestBody GetPackagePayRequest request){
        return ResultUtil.success(packageDataService.getPackagePay(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_getPackagePayDetail)
    public Result getPackagePayDetail(@RequestBody GetPackagePayDetailRequest request){
        return ResultUtil.success(voucherDataService.getPackagePayDetail(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_addPackagePay)
    public Result addPackagePay(@RequestBody AddPackagePayRequest request){
        EntityPackageType packageType = packageTypeService.findByPackageTypeId(request.getComId(), request.getPackageTypeId());
        if (null == packageType) {
            return ResultUtil.error(ResultEnum.Err_1001);
        }
        Date startDate = request.getStartDate();
        Date stopDate = null;
        Date now = new Date();
        switch (packageType.getDayMonth()){
            case 1:
                stopDate = DateUtils.addDays(request.getStartDate(), (packageType.getDaysMonths() - 1));
                break;
            case 10:
                stopDate = DateUtils.addDays(DateUtils.addMonths(request.getStartDate(), packageType.getDaysMonths()), -1);
                break;
        }

        String orderNo = DateUtils.getStrDate(now, DateUtils.YYYYMMDDHHMMSS) + request.getPId() + new Random().nextInt(10000);

        EntityPackageData entityPackageData = new EntityPackageData();
        entityPackageData.setComId(request.getComId());
        entityPackageData.setPId(request.getPId());
        entityPackageData.setPackageTypeId(request.getPackageTypeId());
        entityPackageData.setAmount(packageType.getPrice());
        entityPackageData.setOrderNo(orderNo);
        entityPackageData.setStartDate(startDate);
        entityPackageData.setStopDate(stopDate);

        packageDataService.save(entityPackageData);

        BatchSaveByPackageVoucherDto voucherDto = new BatchSaveByPackageVoucherDto();
        voucherDto.setComId(request.getComId());
        voucherDto.setPId(request.getPId());
        voucherDto.setOrderNo(orderNo);
        voucherDto.setStartDate(startDate);
        voucherDto.setStopDate(stopDate);
        voucherDto.setPackageTypeId(request.getPackageTypeId());
        Integer i = voucherDataService.batchSaveByPackageVoucher(request.getComId(), voucherDto);

        return ResultUtil.success(i);
    }

}
